package gui;

import sistema.Sistema;

import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QLineEdit.EchoMode;

import excecoes.ContaException;
import excecoes.IPException;
import excecoes.PessoaException;
import excecoes.UsuarioException;
/**
 * Classe que muda os dados do usuario.
 *
 */
public class ChangeAccountData extends QDialog {
	
	private Sistema sistema;
	private QLineEdit nameField;
	private QLineEdit usernameField;
	private QLineEdit emailField;
	private QLineEdit passwordField;
	private QLineEdit confirmField;
	private QLineEdit phoneField;

	private final String IMG_PATH = "ImHereM03/img/";
	private QVBoxLayout layout;
	private QPushButton okButton;
	private QPushButton cancelButton;
	private String username;
	private Object parent;
	
	/**
	 * Construtor
	 * @param parent
	 * @param username
	 * @param sistema
	 * @throws ContaException
	 */
	public ChangeAccountData(QWidget parent,String username, Sistema sistema) throws ContaException {
		this.sistema = sistema;
		this.username = username;
		this.parent = parent;
		
		this.setStyleSheet("background: url("+IMG_PATH + "background.png);");

		this.nameField = new QLineEdit(sistema.getAtributoUsuario(username, "nome"));
		this.emailField = new QLineEdit(sistema.getAtributoUsuario(username, "email"));
		this.passwordField = new QLineEdit(sistema.getAtributoUsuario(username, "senha"));
		this.passwordField.setEchoMode(EchoMode.Password);
		this.confirmField = new QLineEdit(sistema.getAtributoUsuario(username, "senha"));
		this.confirmField.setEchoMode(EchoMode.Password);
		this.phoneField = new QLineEdit(sistema.getAtributoUsuario(username, "phone"));
		this.sistema = sistema;

		Object[][] elements = { { this.nameField, "Nome" },			
				{ this.emailField, "Email" },
				{ this.passwordField, "Senha" },
				{ this.confirmField, "Confirmar Senha" },
				{ this.phoneField, "Telefone" } };

		this.layout = new QVBoxLayout();

		for (int i = 0; i < elements.length; i++) {
			QHBoxLayout subLayout = new QHBoxLayout();

			QLabel description = new QLabel((String) elements[i][1]);

			subLayout.addWidget(description);
			subLayout.addWidget((QWidget) elements[i][0]);

			this.layout.addLayout(subLayout);
			this.layout.addSpacing(10);
		}

		this.layout.addSpacing(50);
		this.layout.addLayout(createFinalizeOptions());
		this.layout.addStretch(4);

		this.setLayout(this.layout);
		this.setWindowIcon(new QIcon(IMG_PATH + "icon.png"));
		this.setWindowTitle("Mudar dados de "+username);
	}
	
	
	/**
	 * Cria os botoes de cancelar e confirmar operacao
	 * @return
	 */
	private QHBoxLayout createFinalizeOptions() {
		QHBoxLayout layout = new QHBoxLayout();

		this.cancelButton = new QPushButton("Cancel");
		this.okButton = new QPushButton("Ok");

		layout.addWidget((QWidget) this.cancelButton);
		layout.addSpacing(10);
		layout.addWidget((QWidget) this.okButton);
		
		this.okButton.clicked.connect(this, "changeAccount()");
		this.cancelButton.clicked.connect(this, "close()");

		return layout;

	}

	
	
	/**
	 * Atualiza informacoes cadastrais
	 * @throws ContaException
	 * @throws IPException
	 * @throws PessoaException
	 * @throws UsuarioException
	 */
	private void changeAccount() throws ContaException, IPException, PessoaException, UsuarioException {
		
		String[] atributos = {"nome","email","telefone","senha"};
		QLineEdit[] novosValores = {this.nameField,this.emailField,this.phoneField,this.passwordField};
		

		if (!(this.passwordField.text().equals(this.confirmField.text()))) {
			QMessageBox.critical(this, "Erro", "Senhas nao Conferem");
			return;
		}
		
		for(int i = 0; i <atributos.length; i++) {
			this.sistema.atualizarUsuario(this.username, atributos[i], novosValores[i].text());
		}
		
		QMessageBox.information(this, "Sucesso", "Dados Atualizados com Sucesso");
		((QWidget) this.parent).setWindowTitle("I'mHere - Bem vindo, "
				+ sistema.getAtributoUsuario(username, "nome"));
		this.close();
		
	}
}
